<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<style>
</style>

<div class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center',border:false">
        <a href="${ctxPath}/process/process-definition?processDefinitionId=${proceDefId}&resourceType=image" target="_blank" class="easyui-linkbutton" data-options="iconCls:'fi-photo icon-blue'">查看流程图片</a>
        <table id="tt">
        </table>
    </div>
</div>
<script type="text/javascript">
    var editIndex;
    $(function(){
        var globalVal;
        var datas={};
        $.ajax({
            async:false,
            url: '${ctxPath}/element/getElementDefsByProceDefId/${proceDefId}',
            method:'get',
            dataType:'json',
            success:function(data){
                datas=data;
            }
        })
        $('#tt').datagrid({
            width:'100%',
            height:250,
            singleSelect:true,
            columns:[[
                {field:'name',title:'名称',width:250},
                {field:'key',title:'KEY值',width:100},
                {field:'elementType',title:'类型',width:100},
                {field:'procdefId',title:'流程定义',width:100},
                {field:'print',title:'是否打印',width:50,
                    formatter:function(value){
                       if(value){
                           return "是";
                       }else{
                           return "否";
                       }
                    },
                    editor:{
                        type:'combobox',
                        options:{
                            valueField:'id',
                            textField:'name',
                            data:[{id:true,name:"是"},{id:false,name:"否"}],
                            required:true,
                            onLoadSuccess:function(data){
                                if(editIndex!='undefined'){
                                    if(ed!=null){
                                        var ed = $('#tt').datagrid('getEditor', { index: editIndex, field: 'print' });  //editIndex编辑时记录下的行号
                                        $(ed.target).textbox(globalVal)
                                    }
                                }
                            },
                            onSelect : function(record){
                                if(editIndex!='undefined'){
                                    if(ed!=null){
                                        var ed = $('#tt').datagrid('getEditor', { index: editIndex, field: 'print' });  //editIndex编辑时记录下的行号
//                                var edUnitId = $("#tt").datagrid('getEditor',{
//                                    index : index,
//                                    field : 'print',
//                                });
//                                $(edUnitId.target).val(record.id);
                                        //为 unitTreeName赋值
                                        globalVal= record.name;
                                        $(ed.target).val(record.id);
//                                        $(ed.target).combobox('setValue',record.id);
                                        console.log(record.id)
                                    }
                                }

                            }


                        }
                    }
                },

                {field:'seq',title:'顺序',width:100,
                    editor:'numberbox'
                },
                {field:'action',title:'操作',width:70,align:'center',
                    formatter:function(value,row,index){
                        if (row.editing){
                            var s = '<a href="#" onclick="saverow('+index+')">保存</a> ';
                            var c = '<a href="#" onclick="cancelrow('+index+')">取消</a>';
                            return s+c;
                        } else {
                            var e = '<a href="#" onclick="editrow('+index+')">编辑</a> ';
                            return e;
                        }
                    }
                }
            ]],
            onBeforeEdit:function(index,row){
                row.editing = true;
                $('#tt').datagrid('refreshRow', index);
                editcount++;
            },
            onAfterEdit:function(index,row){
                row.editing = false;
                $('#tt').datagrid('refreshRow', index);
                editcount--;
            },
            onCancelEdit:function(index,row){
                row.editing = false;
                $('#tt').datagrid('refreshRow', index);
                editcount--;
            }
        }).datagrid('loadData',datas).datagrid('acceptChanges');
    });
    var editcount = 0;
    function editrow(index){
        $('#tt').datagrid('beginEdit', index);
    }
    function deleterow(index){
        $.messager.confirm('确认','是否真的删除?',function(r){
            if (r){
                $('#tt').datagrid('deleteRow', index);
            }
        });
    }
    function saverow(index){
        editIndex=index;
        $('#tt').datagrid('endEdit', index);
        $("#tt").datagrid("selectRow",index);
        var row = $("#tt").datagrid("getSelected");
        console.log(row)
        $.ajax({
            type:"post",dataType:"json",
            url:"${ctxPath}/element/UpdateElementDef",
            data:{id:row.id,print:row.print,seq:row.seq},
            success:function(data){
                if(data.success) {
                    showMsg(data.msg);
                    $('#tt').datagrid('reload');

                }else{
                    $.messager.alert("错误",data.msg,"error");
                }
            }
        });

    }
    function cancelrow(index){
        $('#tt').datagrid('cancelEdit', index);
    }


</script>